import { KEY_PREFIX } from "@/constant/index"

const Storage = {
  KEYS: {
    USER: `${KEY_PREFIX}USER`,
    USER_ID: `${KEY_PREFIX}USER_ID`,
    ACCOUNT_INFO: `${KEY_PREFIX}ACCOUNT_INFO`,
    CHANNEL: `${KEY_PREFIX}CHANNEL`,
    PLATACCOUNT: `${KEY_PREFIX}PLATACCOUNT`,
    PLATACCOUNT_ID: `${KEY_PREFIX}PLATACCOUNT_ID`,
    // CHANNEL:`${KEY_PREFIX}CHANNEL`,
    COOKIE: `${KEY_PREFIX}COOKIE`,

  },
  get(key: string, isSession?: boolean) {
    if (!this.isLocalStorage()) {
      return undefined;
    }
    const value = this.getStorage(isSession).getItem(KEY_PREFIX + key);
    if (value) return JSON.parse(value);
    return undefined;
  },

  set(key: string, value: any, isSession?: boolean) {
    if (!this.isLocalStorage()) {
      return undefined;
    }
    value = JSON.stringify(value || "");
    this.getStorage(isSession).setItem(KEY_PREFIX + key, value);
    return true;
  },

  remove(key: string, isSession?: boolean) {
    if (!this.isLocalStorage()) return undefined;
    this.getStorage(isSession).removeItem(KEY_PREFIX + key);
    return true;
  },

  removeAll(keys: any = []) {
    keys.concat(["TOKEN"]).forEach((item: string) => this.remove(item));
  },

  getStorage(isSession?: boolean) {
    return isSession ? sessionStorage : localStorage;
  },

  isLocalStorage() {
    try {
      if (!localStorage) {
        return false;
      }
      return true;
    } catch (e) {
      return false;
    }
  }
};
export default Storage;
